package com.yglab.sqpoll.android.settings;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import com.yglab.sqpoll.android.R;
import com.yglab.sqpoll.android.R.id;
import com.yglab.sqpoll.android.R.layout;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.preference.Preference;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.app.SherlockPreferenceActivity;
import com.actionbarsherlock.view.MenuItem;

/**
 * The activity for Version
 * 
 * @author HongGyun Bae
 */
public class VersionActivity extends SherlockActivity{
	
	Button mUpdateButton;
	TextView mCurrent; //Current information of the version
	TextView mLatest;  //Latest information of the version
	
	DownThread thread;
	String serverVersion;
	
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE); //remove title bar
		setContentView(R.layout.version);
		
		//Setting the ActionBar Sherlock
		ActionBar actionBar = getSupportActionBar();
		actionBar.setDisplayShowTitleEnabled(true);
		actionBar.setHomeButtonEnabled(true);
		actionBar.setDisplayHomeAsUpEnabled(true);
		
		//Setting the current version
		mCurrent = (TextView)findViewById(R.id.current);
		mCurrent.setText(getVersionName(this));
		
		thread = new DownThread("http://blog.naver.com/bhk0722");
		thread.start();
		
		serverVersion = thread.mResult;
		
		mLatest = (TextView)findViewById(R.id.latest);
		mLatest.setText(serverVersion);
		
		mUpdateButton = (Button)findViewById(R.id.btnUpdate);
		if(mCurrent.getText().equals(mLatest.getText()))
			mUpdateButton.setVisibility(0);
		else
			mUpdateButton.setVisibility(1);
	}
	
	/**
	 * Back button using Actionbar Sherlock.
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case android.R.id.home:
			Intent intent = new Intent(this,SettingsActivity.class);
			startActivity(intent);
			return true;
		}
				
		return super.onOptionsItemSelected(item);
	}
	
	/**
	 * Return the version of SQ_poll
	 * @param context
	 * @return
	 */
	public static String getVersionName(Context context){
		try{
			PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPackageName(),
					0);
			return pi.versionName;
		} catch(NameNotFoundException e){
			return null;
		}
	}
}

/***
 * Get the latest version from the web page
 * @author user
 *
 */
class DownThread extends Thread {
	String mAddr;
	String mResult;

	DownThread(String addr) {
		mAddr = addr;
		mResult = "";
	}

	public void run() {

		StringBuilder html = new StringBuilder(); 

		try {
			URL url = new URL(mAddr);
			HttpURLConnection conn = (HttpURLConnection)url.openConnection();

			if (conn != null) {
				conn.setConnectTimeout(10000);
				conn.setUseCaches(false);
				if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
					BufferedReader br = new BufferedReader(
							new InputStreamReader(conn.getInputStream()));

					Boolean bVersion = false;
					for (;;) {
						String line = br.readLine();

						if(bVersion){
							mResult = line;
							break;
						}
						if(line.equals("version:")){
							bVersion = true;
						}
						if (line == null) break;
					}
					br.close();
				}
				conn.disconnect();
			}
		} 

		catch (Exception ex) {;}
		//mAfterDown.sendEmptyMessage(0);
	}
}

